{
Traviesadora beta 0.2.0.13.33 (30.11.2008)
LastMod: 18.12.2008 by Prof1983

Copyright 2008, travieorama@googlemail.com

This file is part of the taviesadora project

taviesadora is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

taviesadora is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
}
unit LoadJabberServer;

interface

uses
  Classes, StrUtils, XMLTag, XMLParser, IdTCPClient, IdTCPConnection, IdHTTP;

type
  TLoadJabberServer = class(TThread)
  protected
    FServerList: TStringList;
    procedure Execute(); override;
  public
    constructor Create();
    destructor Destroy(); override;
    property ServerList: TStringList read FServerList;
  end;

implementation

{ TLoadJabberServer }

constructor TLoadJabberServer.Create();
begin
  inherited Create(True);
  FServerList := TStringList.Create();
end;

destructor TLoadJabberServer.Destroy();
begin
  FServerList.Free();
  inherited Destroy();
end;

procedure TLoadJabberServer.Execute();
var
  XMLFile: WideString;
  //XMLTag: TXMLTag;
  //servers: TXMLTag;
  //serverXML: TXMLTag;
  //XMLParser: TXMLTagParser;
  s: WideString;
  serverName: WideString;
  i: Integer;
  n: Integer;
  //cursor: Integer;
  //success: Boolean;
  IdHTTP: TIdHTTP;
begin
  // http://www.jabberes.org/servers/
  IdHTTP := TIdHTTP.Create(nil);
  try
    XMLFile := IdHTTP.Get('http://www.jabberes.org/servers/servers_by_times_online.html');
    Delete(XMLFile, 1, Pos('<td class=''server''>', XMLFile)-1);
    n := Pos('<td class=''server''>',XMLFile);
    //Cursor := 1;
    FServerList.Clear;
    while (n > 0) and not Terminated do
    begin
      n := Pos('<td class=''server''>', XMLFile);
      Delete(XMLFile,1,n-1+Length('<td class=''server''>'));
      n := pos('<a name=''',XMLFile)+ length('<a name=''');
      ServerName := '';
      for i := n to Length(XMLFile) do
      begin
        if (XMLFile[i] = '''') then
          Break;
        ServerName := ServerName + XMLFile[i];
      end;
      //'<td class=''offline_since''>26-May-2008 18:32 UTC</td>'
      n := Pos('offline_since''>', XMLFile) + Length('offline_since''>') ;
      s := '';
      for i := n to length(XMLFile) do
      begin
        if (XMLFile[i] = '<') then
          Break;
        s := s + XMLFile[i];
      end;
      if (s = '') then
      begin
        n := Pos('times_online''>', XMLFile) + Length('times_online''>');
        s := '';
        for i := n to Length(XMLFile) do
        begin
          if XMLFile[i] = '<' then
            Break;
          s := s + XMLFile[i];
        end;
        //Cursor := i;
        n := Pos('(',s)+1;
        s := MidStr(s,n,Pos(')',s)-n);
        FServerList.Add(ServerName+#9+s);
      end;
      n := Pos('<td class=''server''>', XMLFile);
      Delete(XMLFile,1,n-1);
      n := Pos('<td class=''server''>', XMLFile);
    end;
  finally
    IdHTTP.free;
  end;
end;

end.
